package com.smart.home.util;

import java.io.ByteArrayOutputStream;

/**
 * 执行命令编码工具
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-09
 */
public abstract class CommandEncoder {

    /**
     * 对发送数据进行编码
     *
     * @param bytes
     * @return
     */
    public static byte[] decode(String bytes) {
        bytes = bytes.toUpperCase();
        String hexString = "0123456789ABCDEF";
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
        try {
            baos.reset();
            // 将每2位16进制整数组装成一个字节
            for (int i = 0; i < bytes.length(); i += 2) {
                baos.write((hexString.indexOf(bytes.charAt(i)) * 16 + hexString.indexOf(bytes.charAt(i + 1))));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

}
